<?php
/**
 * Guard 辅助方法
 *
 * Author: Zeon
 * Date: 2017/11/14
 * Time: 17:19
 * Created by PhpStorm.
 */
namespace App\Services\Utils;


use App\Models\Admin\CustomerAdmin;
use App\Models\Admin\PanxAdmin;
use App\Models\Admin\StoreAdmin;
use App\Models\Customer\Customer;
use App\Models\Customer\DeviceAccount;
use App\Models\Store\Store;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Gate;
use Tymon\JWTAuth\Contracts\JWTSubject;

class Guard
{
    const ADMIN_PANX = PanxAdmin::GUARD_NAME;

    const ADMIN_STORE = StoreAdmin::GUARD_NAME;

    const ADMIN_CUSTOMER = CustomerAdmin::GUARD_NAME;

    const APP = DeviceAccount::GUARD_NAME;

    const WECHAT = 'wechat'; // 微信并没有什么 Guard
    /**
     * Delegate Gate Method On Auth::guard
     * @example
     *      Guard::matchId($admin_model, $operate_model)
     *
     * @param  string  $method
     * @param  mixed[2]  $parameters
     * @return mixed
     */
    public static function __callStatic($method, array $parameters)
    {
        list($admin, $arguments) = $parameters;

        return Gate::forUser($admin)->check($method, $arguments);
    }

    /**
     * @return PanxAdmin|null
     */
    public static function panxAdmin(): ?PanxAdmin
    {
        return Auth::guard(self::ADMIN_PANX)->user();
    }

    public static function isPanxSuperAdmin(): bool
    {
        $admin = self::panxAdmin();

        return $admin ? $admin->isSuperMan() : false;
    }

    /**
     * @return CustomerAdmin|null
     */
    public static function customerAdmin(): ?CustomerAdmin
    {
        return Auth::guard(self::ADMIN_CUSTOMER)->user();
    }

    /**
     * @return \App\Models\Customer\Customer
     */
    public static function customer(): ?Customer
    {
        return self::customerAdmin()->customer ?? null;
    }

    /**
     * @test
     *
     * @return bool
     */
    public static function isCustomerSuperAdmin(): bool
    {
        $admin = self::customerAdmin();

        return $admin ? $admin->isSuperMan() : false;
    }

    /**
     * @return StoreAdmin|null
     */
    public static function storeAdmin(): ?StoreAdmin
    {
        return Auth::guard(self::ADMIN_STORE)->user();
    }

    /**
     * @return bool
     */
    public static function isStoreSuperAdmin(): bool
    {
        $admin = self::storeAdmin();

        return $admin ? $admin->isSuperMan() : false;
    }

    /**
     * @return Store|null
     */
    public static function store(): ?Store
    {
        return self::storeAdmin()->store;
    }

    /**
     * @return DeviceAccount|null
     */
    public static function deviceAccount(): ?DeviceAccount
    {
        return Auth::guard(self::APP)->user();
    }

    /**
     * @return CustomerAdmin|PanxAdmin|StoreAdmin|DeviceAccount|null
     */
    public static function operatorUser(): ?JWTSubject
    {
        return self::panxAdmin()
            ?: self::customerAdmin()
            ?: self::storeAdmin()
            ?: self::deviceAccount()
            ?: null;
    }

    /**
     * @return bool
     */
    public static function hasAdmin(): bool
    {
        static $has_admin;

        if (! isset($has_admin)) {
            $has_admin = self::panxAdmin() || self::storeAdmin() || self::customerAdmin();
        }

        return $has_admin;
    }
}